Barcodes & QRs in C# & VB.NET Applications
Reading and writing barcodes in C# and all other .NET languages is easy using the Iron Barcode software library from Iron Software.
Install IronBarcode
The first step in the journey will be installing Iron Barcode, which may be achieved by using our NuGet package or by downloading the DLL from this website. of the Iron Barcode classes can be found in the IronBarcode
namespace.
The easiest way to install IronBarcode is using the NuGet Package Manager for Visual-Studio: The package name is “Barcode”.
Install-Package BarCode
https://www.nuget.org/packages/Barcode/
Reading a Barcode or QR Code
Reading a barcode with Iron Barcode simply takes a few lines of code.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-1.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("QuickStart.jpg");
if (results != null)
{
foreach (BarcodeResult result in results)
{
Console.WriteLine(result.Text);
}
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("QuickStart.jpg")
If results IsNot Nothing Then
For Each result As BarcodeResult In results
Console.WriteLine(result.Text)
Next result
End If
To improve speed and accuracy of reading barcodes, we can specify a specific barcode format (or formats) so that Iron Barcode doesn't have to try for every single type of barcode known. We may also pick a specific crop region or area of an image to look in, if that is known.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-2.cs
using IronBarCode;
BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128,
CropArea = new System.Drawing.Rectangle(100, 200, 300, 400),
};
BarcodeResults result = BarcodeReader.Read("QuickStart.jpg", myOptionsExample);
if (result != null)
{
Console.WriteLine(result.First().Text);
}
Imports IronBarCode
Private myOptionsExample As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
.CropArea = New System.Drawing.Rectangle(100, 200, 300, 400)
}
Private result As BarcodeResults = BarcodeReader.Read("QuickStart.jpg", myOptionsExample)
If result IsNot Nothing Then
Console.WriteLine(result.First().Text)
End If
We may also choose to scan for multiple barcodes in a simple scan.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-3.cs
using IronBarCode;
BarcodeResults results = BarcodeReader.Read("MultipleBarcodes.png");
// Loop through the results
foreach (BarcodeResult result in results)
{
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
Imports IronBarCode
Private results As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.png")
' Loop through the results
For Each result As BarcodeResult In results
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
We may also use Iron Barcode to read barcodes from one or multiple pages of PDF documents or multipage TIFFs.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-4.cs
using IronBarCode;
BarcodeResults pagedResults = BarcodeReader.Read("MultipleBarcodes.pdf");
// Loop through the results
foreach (BarcodeResult result in pagedResults)
{
int pageNumber = result.PageNumber;
string value = result.Value;
Bitmap img = result.BarcodeImage;
BarcodeEncoding barcodeType = result.BarcodeType;
byte[] binary = result.BinaryValue;
Console.WriteLine(result.Value);
}
// or from a multi-page TIFF scan with image correction:
BarcodeResults multiFrameResults = BarcodeReader.Read(inputImage: "Multiframe.tiff", new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
Multithreaded = false,
RemoveFalsePositive = false,
ImageFilters = null
});
Imports IronBarCode
Private pagedResults As BarcodeResults = BarcodeReader.Read("MultipleBarcodes.pdf")
' Loop through the results
For Each result As BarcodeResult In pagedResults
Dim pageNumber As Integer = result.PageNumber
Dim value As String = result.Value
Dim img As Bitmap = result.BarcodeImage
Dim barcodeType As BarcodeEncoding = result.BarcodeType
Dim binary() As Byte = result.BinaryValue
Console.WriteLine(result.Value)
Next result
' or from a multi-page TIFF scan with image correction:
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read(inputImage:= "Multiframe.tiff", New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.Multithreaded = False,
.RemoveFalsePositive = False,
.ImageFilters = Nothing
})
Writing Barcodes
To create or write barcodes using Iron Barcode, we use the BarcodeWriter class. With BarcodeWriter, writing barcodes is extremely easy. We simply specify the barcode format and the value we wish to express, and it will create an image which may be exported as a System.Drawing.Image object or saved to a file.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-5.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
Styling Barcodes
The visual depiction of a barcode can be manipulated very easily using Iron Barcode. The output of every BarcodeWriter operation is a generated barcode object. This generated barcode object has a Fluent API, allowing the graphical settings of the barcode to be set in a single line of code, similar to Linq.
Popular features for styling barcodes include resizing barcodes, setting margins, changing background colors, changing barcode colors, and verifying that the output barcode is still readable.
In a similar fashion, we may also add annotations such as text to a barcode in any typeface we want, or place the value of the barcode very easily under or above the barcode.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-6.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SaveAsImage("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SaveAsImage("myBarcode.png")
Exporting Barcodes as HTML
The generated barcode object also has a convenient feature to export HTML from a generated barcode. It can export a barcode as a standalone HTML document with no external assets, as a standalone HTML tag, or as a data URI.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-7.cs
using IronBarCode;
GeneratedBarcode myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.AddAnnotationTextAboveBarcode("Product URL:");
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(100);
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple);
// All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png");
Imports IronBarCode
Private myBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.AddAnnotationTextAboveBarcode("Product URL:")
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(100)
myBarcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Purple)
' All major image formats supported as well as PDF and HTML
myBarcode.SaveAsPng("myBarcode.png")
Generating QR Codes
When generating QR codes using Iron Barcode, we may opt to use the QR Code Writer class instead of the BarcodeWriter class. This class gives some new and interesting features for QR code writing. It allows us to set the QR error correction level, allowing you to decide on a balance between the size of your QR code and its ease of readability.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-8.cs
using IronBarCode;
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf");
Imports IronBarCode
QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPdf("MyQR.pdf")
Iron Barcode also allows for QR codes to be stylized, such as having a logo graphic placed and snapped to grid in the exact center of the image. It may also be colorized so that it matches a certain brand or graphic identity.
:path=/static-assets/barcode/content-code-examples/get-started/get-started-9.cs
using IronBarCode;
using IronSoftware.Drawing;
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf");
Imports IronBarCode
Imports IronSoftware.Drawing
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/csharp/barcode/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(Color.DarkGreen).SaveAsPdf("MyQRWithLogo.pdf")
Supported Barcode Formats
Iron Barcode supports a large number of commonly used barcode formats:
- QR codes, including decorated and branded QR codes with logos and colors
- Multi-format barcodes, such as Aztec, Data Matrix, CODE 93, and CODE 128
- RSS Expanded Databar, UPS MaxiCode, and USPS IMb OneCode barcodes
- Stacked linear barcodes such as RSS-14 and PDF-417
- Conventional numerical barcode formats such as UPCA, UPCE, EAN-8, EAN-13, Codabar, ITF, MSI, and Plessey
:path=/static-assets/barcode/content-code-examples/get-started/get-started-10.cs
using IronBarCode;
var myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128);
myBarcode.SaveAsImage("myBarcode.png");
myBarcode.SaveAsGif("myBarcode.gif");
myBarcode.SaveAsHtmlFile("myBarcode.html");
myBarcode.SaveAsJpeg("myBarcode.jpg");
myBarcode.SaveAsPdf("myBarcode.Pdf");
myBarcode.SaveAsPng("myBarcode.png");
myBarcode.SaveAsTiff("myBarcode.tiff");
myBarcode.SaveAsWindowsBitmap("myBarcode.bmp");
Image myBarcodeImage = myBarcode.Image;
Bitmap myBarcodeBitmap = myBarcode.ToBitmap();
string dataUrl = myBarcode.ToDataUrl();
string imgTagForHtml = myBarcode.ToHtmlTag();
byte[] pngBytes = myBarcode.ToPngBinaryData();
// Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
using (System.IO.Stream pdfStream = myBarcode.ToPdfStream())
{
// Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
}
myBarcode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50);
Imports IronBarCode
Private myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeEncoding.Code128)
myBarcode.SaveAsImage("myBarcode.png")
myBarcode.SaveAsGif("myBarcode.gif")
myBarcode.SaveAsHtmlFile("myBarcode.html")
myBarcode.SaveAsJpeg("myBarcode.jpg")
myBarcode.SaveAsPdf("myBarcode.Pdf")
myBarcode.SaveAsPng("myBarcode.png")
myBarcode.SaveAsTiff("myBarcode.tiff")
myBarcode.SaveAsWindowsBitmap("myBarcode.bmp")
Dim myBarcodeImage As Image = myBarcode.Image
Dim myBarcodeBitmap As Bitmap = myBarcode.ToBitmap()
Dim dataUrl As String = myBarcode.ToDataUrl()
Dim imgTagForHtml As String = myBarcode.ToHtmlTag()
Dim pngBytes() As Byte = myBarcode.ToPngBinaryData()
' Binary barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
Using pdfStream As System.IO.Stream = myBarcode.ToPdfStream()
' Stream barcode image output also works for GIF,JPEG, PDF, PNG, BMP and TIFF
End Using
myBarcode.StampToExistingPdfPage("ExistingPDF.pdf", 1, 200, 50)
Why Choose Iron Barcode?
Iron Barcode features a friendly API for developers to read and write barcodes for .NET, which optimizes for accuracy and a low error rate in real world use-cases.
The BarcodeWriter class, for example, will automatically validate and correct 'checksums' on UPCA and UPCE barcodes. It will also 'zero-pad' numbers which are too short for a specific numeric format. If your data is inappropriate for the specified data format, Iron Barcode will instruct the developer of a more appropriate barcode format which they might use.
Iron Barcode shines in the area of reading barcodes when the barcode has been scanned or has been read from a photographic image. In other words, the image is not graphically perfect and is not just simply a machine-generated screenshot.
Iron Barcode includes automatic rotation and perspective correction, correction for digital noise, and can automatically detect the type of barcode which is encoded in an image.
Moving Forward
To get more out of Iron Barcode, we encourage you to read the tutorials within this documentation section, to visit us on GitHub, and to read the .NET API Reference in an MSDN format.